COPYRIGHT = Copyright (c) 2011-2012,2018-2022,2024, the ACME Software Deli

# ============================================================================
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
#
#   Permission to use for any purpose, modify, copy, and make enhancements
#   and derivative works of the software is granted if attribution is given to
#   R.M. Gillmore, dba the ACME Software Deli, as the author
#
#   While the ACME Software Deli does not work for money, there is nonetheless a
#   a significant amount of work involved.  The ACME Software Deli maintains the
#   rights to all code written, though it may be used and distributed as long as
#   the following conditions are maintained.
#
#   1.  The copyright statement at the top of each code block is maintained in
#       your distribution.
#   2.  You do not identify yourself as the ACME Software Deli
#   3.  Any changes made to the software are sent to the ACME Software Deli
#   4.  You distribute your code and any modifications to my code freely.
#   5.  There is no charge for your work which uses this library
# ============================================================================

#
# @(#) $Id: Makefile 927 2024-02-29 21:52:39Z rmgillmore $
#

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	definitions which allow the Makefile to work with 32-bit and 64-bit
#	versions of Windows, and to allow this Makefile to work regardless of the
#	location of this file on the disk

SHELL = cmd.exe
# OSARCH = $(shell osArch.cmd)
MAKEFILE = Makefile
MAKEFILE_DEPENDS = $(MAKEFILE).dep
MAKEFILE_SOURCES = $(MAKEFILE).sources
FLAGFILE = $(MAKEFILE).flg
:WORK: = $(HOME)\Documents\plm86
LOCAL_UTILS = $(shell back2front $(UTILS))
INTEL_TOOLS = $(LOCAL_UTILS)/plm86Tools
MS4TOOLS = $(HOME)/Documents/utils/masm4
MSTOOLS = $(HOME)/Documents/utils/masm510/bin
SUPPORT_DIR = $(LOCAL_UTILS)/source
TEST_DIR = TestApps
EIGHT_DOT_3_SCRIPT = 8dot3path.cmd


SHORT_UTILS = $(shell $(EIGHT_DOT_3_SCRIPT) $(UTILS))
PLM86_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(INTEL_TOOLS))
MASM4_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(MS4TOOLS))
MASM5_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(MSTOOLS))
HOME_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(HOMEPATH)\Documents)
REM_SUBSTR = $(SUPPORT_DIR)/rmSubStr.exe
CWD = $(shell pwd)
CWD_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(CWD))
APPS_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(shell parentDir.cmd) )\Apps
CURRENT_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(CWD_8DOT3) $(HOME_8DOT3))
UTILS_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(SHORT_UTILS) $(HOME_8DOT3))
PLM86T_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(PLM86_TOOLS) $(HOME_8DOT3))
MASM4T_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(MASM4_TOOLS) $(HOME_8DOT3))
MASMT_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(MASM5_TOOLS) $(HOME_8DOT3))
DOS_APPS_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(APPS_8DOT3) $(HOME_8DOT3))
DOS_PATH = $(UTILS_8DOT3);$(PLM86T_8DOT3);$(MASM4T_8DOT3);$(MASMT_8DOT3)

ECHO = echo
ACME_OBJ = obj
ARCH = exe
INCLUDE = .

DEL = del /f /q
MOVE = ren
DOS = vDos
DOS_INIT_FILE = autoexec.txt
DOS_CONFIG_FILE = config.txt
DOS_SCRIPT = vDosWork.bat

# ASM = $(MSTOOLS)/masm /Zi
# ASM = masm /Zi

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	if we want to distribute the executables with Logging enabled, run make as:
#		make Logging=1 <other options>

ifdef Logging
	doLogging = set(LOGGING)
else
	doLogging=
endif

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#
#	when building a development version of the library, run make as:
#		make clean all
#	this turns on debugging symbols, and logging, but no ID strings
#
#	when building a release version of the library, run make as:
#		make DoRelease=1 clean all
#	this turns off debugging symbols, logging, and turns on ID strings
#
# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_

ifdef DoRelease
	ASM = masm /DSMALL
	DEBUG_FLAG = set(noID=1)
else
	ASM = masm /Zi
	DEBUG_FLAG = debug set(noID=0) $(doLogging)
endif

LIB = tlib
PREPROC = ..\plmPreProc.py
PARSEOUT = $(SUPPORT_DIR)/parseOutput.py

PLM_LIBRARY =		plm86.lib
ACME_LIBRARY =		$(START_OBJ) $(SHORT_START_OBJ) $(TINY_START_OBJ) $(PLM_LIBRARY)
START_OBJ = 		asmStart.$(ACME_OBJ)
SHORT_START_OBJ =	shrtStrt.$(ACME_OBJ)
TINY_START_OBJ =	tinyStrt.$(ACME_OBJ)

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	the object files in this list are in the library
#
#	*** EXCEPTION ***
#		asmStart and shrtStrt are used by all applications (with a few
#		exceptions).  by placing them in the library list, they will always be
#		up to date, even though they are not extraced from the library as part
#		of the build

LIB_OBJS = \
	$(START_OBJ) \
	$(SHORT_START_OBJ) \
	$(TINY_START_OBJ) \
	absDRead.$(ACME_OBJ) \
	arrayMgr.$(ACME_OBJ) \
	bitArray.$(ACME_OBJ) \
	changExt.$(ACME_OBJ) \
	charStr.$(ACME_OBJ)	\
	chdir.$(ACME_OBJ) \
	clkTick.$(ACME_OBJ) \
	cmdArgs.$(ACME_OBJ) \
	compAsm.$(ACME_OBJ) \
	cprintf.$(ACME_OBJ)	\
	crc32.$(ACME_OBJ) \
	ctype.$(ACME_OBJ) \
	curDir.$(ACME_OBJ) \
	curDisk.$(ACME_OBJ) \
	date.$(ACME_OBJ) \
	dateStr.$(ACME_OBJ) \
	debugHlp.$(ACME_OBJ) \
	dirFunct.$(ACME_OBJ) \
	dirHlpr.$(ACME_OBJ) \
	dirHlpr2.$(ACME_OBJ) \
	dosFind.$(ACME_OBJ) \
	drvLogNm.$(ACME_OBJ) \
	dtaModle.$(ACME_OBJ) \
	errCode.$(ACME_OBJ) \
	exit.$(ACME_OBJ) \
	fattrib.$(ACME_OBJ) \
	fclose.$(ACME_OBJ) \
	fdskbufs.$(ACME_OBJ) \
	feof.$(ACME_OBJ) \
	fgets.$(ACME_OBJ) \
	fhValid.$(ACME_OBJ) \
	fileTree.$(ACME_OBJ) \
	fnmatch.$(ACME_OBJ) \
	fopen.$(ACME_OBJ) \
	fprintf.$(ACME_OBJ) \
	fread.$(ACME_OBJ) \
	fseek.$(ACME_OBJ) \
	fstat.$(ACME_OBJ) \
	ftell.$(ACME_OBJ) \
	fwrite.$(ACME_OBJ) \
	getAt.$(ACME_OBJ) \
	getRegs.$(ACME_OBJ) \
	getenv.$(ACME_OBJ) \
	greetUsr.$(ACME_OBJ) \
	hostName.$(ACME_OBJ) \
	iniMgr.$(ACME_OBJ) \
	isDir.$(ACME_OBJ) \
	keyIn.$(ACME_OBJ) \
	ldXcAsm.$(ACME_OBJ) \
	lfModule.$(ACME_OBJ) \
	lnEditor.$(ACME_OBJ) \
	loadExec.$(ACME_OBJ) \
	logger.$(ACME_OBJ) \
	lqdword.$(ACME_OBJ) \
	matches.$(ACME_OBJ) \
	mem.$(ACME_OBJ) \
	mem4Edit.$(ACME_OBJ) \
	memcmp.$(ACME_OBJ) \
	memcpy.$(ACME_OBJ) \
	memset.$(ACME_OBJ) \
	menuCmds.$(ACME_OBJ) \
	mkdir.$(ACME_OBJ) \
	moddiv.$(ACME_OBJ) \
	morseCod.$(ACME_OBJ) \
	numStrs.$(ACME_OBJ) \
	pClozDir.$(ACME_OBJ) \
	pOpenDir.$(ACME_OBJ) \
	pReadDir.$(ACME_OBJ) \
	pRwdDir.$(ACME_OBJ) \
	parmData.$(ACME_OBJ) \
	plmStart.$(ACME_OBJ) \
	printStr.$(ACME_OBJ) \
	printf.$(ACME_OBJ) \
	prtfEng.$(ACME_OBJ) \
	ptrMath.$(ACME_OBJ) \
	putch.$(ACME_OBJ) \
	qSort.$(ACME_OBJ) \
	qwordDiv.$(ACME_OBJ) \
	regRept.$(ACME_OBJ) \
	rmdir.$(ACME_OBJ) \
	shrt2lng.$(ACME_OBJ) \
	smplName.$(ACME_OBJ) \
	sort.$(ACME_OBJ) \
	sprintf.$(ACME_OBJ) \
	srtFuncs.$(ACME_OBJ) \
	stack.$(ACME_OBJ) \
	startSup.$(ACME_OBJ) \
	str2Nums.$(ACME_OBJ)	\
	strMatch.$(ACME_OBJ)	\
	strSplit.$(ACME_OBJ)	\
	strcat.$(ACME_OBJ)	\
	strchr.$(ACME_OBJ)	\
	strcmp.$(ACME_OBJ)	\
	strcpy.$(ACME_OBJ)	\
	strdup.$(ACME_OBJ)	\
	stricmp.$(ACME_OBJ)	\
	strlen.$(ACME_OBJ)	\
	strlwr.$(ACME_OBJ)	\
	strncat.$(ACME_OBJ)	\
	strncpy.$(ACME_OBJ)	\
	strrchr.$(ACME_OBJ)	\
	strtok.$(ACME_OBJ) \
	strupr.$(ACME_OBJ)	\
	sysCaAsm.$(ACME_OBJ) \
	sysCalls.$(ACME_OBJ) \
	tolower.$(ACME_OBJ) \
	toupper.$(ACME_OBJ) \
	uname.$(ACME_OBJ) \
	vChgAttr.$(ACME_OBJ) \
	vClear.$(ACME_OBJ) \
	vCrOnOff.$(ACME_OBJ) \
	vCurPos.$(ACME_OBJ) \
	vDrawBox.$(ACME_OBJ) \
	vGetAttr.$(ACME_OBJ) \
	vGetChar.$(ACME_OBJ) \
	vGotoXY.$(ACME_OBJ) \
	vInvAt.$(ACME_OBJ) \
	vInvAttr.$(ACME_OBJ) \
	vScrlWin.$(ACME_OBJ) \
	vScrnSz.$(ACME_OBJ) \
	vWrtChAt.$(ACME_OBJ) \
	vWrtChar.$(ACME_OBJ) \
	vWrtMany.$(ACME_OBJ) \
	vWrtStng.$(ACME_OBJ) \
	volInfo.$(ACME_OBJ) \
	winMgr.$(ACME_OBJ) \

#	buffMgr.$(ACME_OBJ) \
#	fhandle.$(ACME_OBJ) \
#	hexASCII.$(ACME_OBJ) \
#	int8Asm.$(ACME_OBJ) \
#	int8Math.$(ACME_OBJ) \
#	keyIn.$(ACME_OBJ) \
#	poolGt.$(ACME_OBJ) \
#	poolLg.$(ACME_OBJ) \
#	poolMed.$(ACME_OBJ) \
#	poolMs.$(ACME_OBJ) \
#	poolSml.$(ACME_OBJ) \
#	poolTiny.$(ACME_OBJ) \
#	vBlock.$(ACME_OBJ) \
#	videoIO.$(ACME_OBJ)	\

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	filename suffixes listed and instructions for each

.SUFFIXES : .$(ACME_OBJ) .p86 .ext .a86 .e86

.a86.$(ACME_OBJ) :
	@echo -- Assembling $<
ifeq ($(OSARCH), 64)
	@if not exist $(DOS_INIT_FILE) make $(DOS_INIT_FILE)
	@if not exist $(DOS_CONFIG_FILE) make $(DOS_CONFIG_FILE)
endif
	@echo @echo off > $(DOS_SCRIPT)
	@echo $(ASM) $<,,; >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@echo exit 0 >> $(DOS_SCRIPT)
	@$(DOS)
else
	@$(DOS_SCRIPT) 1>temp 2>&1
endif
	@if exist temp $(DEL) temp
	@$(DEL) $(DOS_SCRIPT)
	@$(MOVE) $(*F).lst temp
	@$(MOVE) temp $(*F).lst
	@$(MOVE) $@ temp
	@$(MOVE) temp $@

.p86.$(ACME_OBJ) :
	@echo -- Compiling $<
ifeq ($(OSARCH), 64)
	@if not exist $(DOS_INIT_FILE) make $(DOS_INIT_FILE)
	@if not exist $(DOS_CONFIG_FILE) make $(DOS_CONFIG_FILE)
endif
	@$(PREPROC) -of $(*F).ppp -if $< > NUL:
	@$(DEL) plmPreProc*log
	@echo @echo off > $(DOS_SCRIPT)
	@echo plm86 $(*F).ppp $(DEBUG_FLAG) >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@$(DOS)
else
	@$(DOS_SCRIPT) 1>temp 2>&1
endif
	@if exist $(*F).ppp $(DEL) $(*F).ppp
	@$(DEL) $(DOS_SCRIPT)
	@if exist temp $(DEL) temp
	@if exist temp1 $(DEL) temp1
	@$(MOVE) $(*F).lst temp
	@$(MOVE) temp $(*F).lst
	@$(MOVE) $(*F).obj temp1
	@$(MOVE) temp1 $(*F).obj

.PHONY : clean depend report

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	these are the actual target specifications

all:	 $(DOS_INIT_FILE) $(DOS_CONFIG_FILE) $(PLM_LIBRARY)

release:	$(DOS_INIT_FILE) $(DOS_CONFIG_FILE) $(PLM_LIBRARY)
	cleanup -f -n

$(PLM_LIBRARY) :	lib.rsp $(LIB_OBJS)
	@$(LIB) $@ @$< ,plm86.lst

lib.rsp : $(LIB_OBJS)
	@objsToRsp $(LIB_OBJS) > $@

clean :
#	@$(DEL) *.rsp
	@if exist *.$(ACME_OBJ) $(DEL) *.$(ACME_OBJ)
	@if exist temp $(DEL) temp
	@if exist $(DOS_INIT_FILE) $(DEL) $(DOS_INIT_FILE)
	@if exist $(DOS_CONFIG_FILE) $(DEL) $(DOS_CONFIG_FILE)
	@if exist $(8_DOT_3_SCRIPT) $(DEL) $(8_DOT_3_SCRIPT)
	@if exist $(PLM_LIBRARY) $(DEL) $(PLM_LIBRARY)
	@if exist $(FLAGFILE) $(DEL) $(FLAGFILE)
	@if exist $(DOS_SCRIPT) $(DEL) $(DOS_SCRIPT)
	@cleanup -n -f

report :
	@echo home 8.3 $(HOME_8DOT3)
	@echo cwd 8.3 $(CWD_8DOT3)
	@echo current 8.3 $(CURRENT_8DOT3)
	@echo utils 8.3 $(UTILS_8DOT3)
	@echo plmtools 8.3 $(PLM86T_8DOT3)
	@echo masmtools 8.3 $(MASMT_8DOT3)
	@echo appsDir 8.3 $(APPS_8DOT3)
	@echo dosApps 8.3 $(DOS_APPS_8DOT3)

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	specific file dependencies defined

$(8_DOT_3_SCRIPT) : $(MAKEFILE)
	@echo @ECHO OFF> $@
	@echo if '%1'=='' (%0 .) else echo %~s1>> $@

$(DOS_CONFIG_FILE) : $(MAKEFILE)
	@echo Creating $@
	@echo TEXT = 35x95> $@
	@echo FONT = C:\WINDOWS\FONTS\CONSOLA>> $@
#	@echo FILES = 50>> $@

$(DOS_INIT_FILE) : $(MAKEFILE)
	@set TEMP_STR =
	@echo Creating $@
	@echo @echo off> $@
	@echo rem>> $@
	@echo rem	mount the home directory as C, and setup a path>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo use D: C:\vDos>> $@
	@echo use C: $(HOME_8DOT3)>> $@
	@echo set path=$(HOMEDRIVE)\;$(DOS_PATH)>> $@
	@echo set path=$(DOS_APPS_8DOT3);%%path%%>> $@
	@echo.>> $@
	@echo rem>> $@
	@echo rem	change to the directory where we need some work done, and get to it>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo cd $(CURRENT_8DOT3)>> $@
	@echo if exist $(DOS_SCRIPT) call $(DOS_SCRIPT) >> $@
	@echo if exist $(DOS_SCRIPT) exit ERRORLEVEL >> $@
	@echo.>> $@
	@echo rem>> $@
	@echo rem	when you get this far, we will have a command prompt,>> $@
	@echo rem	so make it easier to read>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo dosColor -n >> $@

$(FLAGFILE) : $(MAKEFILE)
	@touch $@ > nul:

depend $(MAKEFILE_DEPENDS) : $(MAKEFILE)
	@$(SUPPORT_DIR)/mkDepend.py -nl -ic -sd . -fs "*.?86;" -id "$(INCLUDE)" -of $(MAKEFILE_DEPENDS)
	@touch $(FLAGFILE) > nul:

##################
include $(MAKEFILE_DEPENDS)

